package cn.com.isfree.tio.server.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.maintain.ChannelContextMapWithLock;
import org.tio.utils.json.Json;

import cn.com.isfree.tio.common.ShowcasePacket;
import cn.com.isfree.tio.common.intf.AbsShowcaseBsHandler;
import cn.com.isfree.tio.common.packets.SyncRespBody;

/**
 * @author tanyaowu
 * 2017年3月27日 下午9:51:28
 */
public class SyncRespHandler extends AbsShowcaseBsHandler<SyncRespBody> {
	private static Logger log = LoggerFactory.getLogger(SyncRespHandler.class);

	/**
	 * @param args
	 * @author tanyaowu
	 */
	public static void main(String[] args) {

	}

	/**
	 *
	 * @author tanyaowu
	 */
	public SyncRespHandler() {
	}

	/**
	 * @return
	 * @author tanyaowu
	 */
	@Override
	public Class<SyncRespBody> bodyClass() {
		return SyncRespBody.class;
	}

	/**
	 * @param packet
	 * @param bsBody
	 * @param channelContext
	 * @return
	 * @throws Exception
	 * @author tanyaowu
	 */
	@Override
	public Object handler(ShowcasePacket packet, SyncRespBody bsBody, ChannelContext channelContext) throws Exception {
		System.out.println("收到点对点同步响应消息:" + Json.toJson(bsBody));
		if (bsBody.getSynSeq() != null && bsBody.getSynSeq() > 0) {
			packet.setSynSeq(bsBody.getSynSeq());
			ChannelContextMapWithLock waitingResps = channelContext.getGroupContext().getWaitingResps();
			waitingResps.put(bsBody.getSynSeq(), packet);
			return null;
		}else {
			throw new RuntimeException("synSeq必须大于0");
		}
	}
}
